package com.tengniu.web.controller.buss;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tengniu.buss.domain.MassageChair;
import com.tengniu.buss.service.MassageChairService;
import com.tengniu.common.annotation.Log;
import com.tengniu.common.buss.enums.YesOrNoEnum;
import com.tengniu.common.core.controller.BaseController;
import com.tengniu.common.core.domain.AjaxResult;
import com.tengniu.common.core.page.TableDataInfo;
import com.tengniu.common.enums.BusinessType;
import com.tengniu.service.MassageChairAdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 按摩椅管理
 */
@RestController
@RequestMapping("/admin/chair")
@Slf4j
public class ChairAdminController extends BaseController {

    @Resource
    private MassageChairService massageChairService;
    @Resource
    private MassageChairAdminService massageChairAdminService;

    /**
     * 获取按摩椅
     */
    @PreAuthorize("@ss.hasPermi('buss:chair:list')")
    @PostMapping("/list")
    public TableDataInfo<MassageChair> list(@RequestBody MassageChair massageChair) {
        startPage();
        List<MassageChair> list = massageChairService.list(
                Wrappers.lambdaQuery(MassageChair.class)
                        .eq(Objects.nonNull(massageChair.getStatus()), MassageChair::getStatus, massageChair.getStatus())
                        .like(Objects.nonNull(massageChair.getMassageChairName()), MassageChair::getMassageChairName, massageChair.getMassageChairName()));
        return getDataTable(list);
    }

    /**
     * 获取按摩椅分账比例
     */
    @PreAuthorize("@ss.hasPermi('buss:chair:list')")
    @GetMapping("/getChairRate")
    public AjaxResult getChairRate() {
        return AjaxResult.success();
    }

    /**
     * 新增按摩椅
     */
    @PreAuthorize("@ss.hasPermi('buss:chair:add')")
    @Log(title = "按摩椅", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody MassageChair MassageChair) {
        return toAjax(massageChairService.save(MassageChair));
    }

    /**
     * 修改按摩椅
     */
    @PreAuthorize("@ss.hasPermi('buss:chair:edit')")
    @Log(title = "按摩椅", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody MassageChair MassageChair) {
        return toAjax(massageChairService.updateById(MassageChair));
    }

    /**
     * 删除按摩椅
     */
    @PreAuthorize("@ss.hasPermi('buss:chair:remove')")
    @Log(title = "按摩椅", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{chairId}")
    public AjaxResult remove(@PathVariable Long chairId) {
        return toAjax(massageChairService.removeById(chairId));
    }

    /**
     * 上架
     */
    @PreAuthorize("@ss.hasPermi('buss:chair:shelf')")
    @Log(title = "按摩椅", businessType = BusinessType.UPDATE)
    @GetMapping("/shelf/{chairId}")
    public AjaxResult shelf(@PathVariable Long chairId) {
        MassageChair massageChair = massageChairService.getById(chairId);
        if (massageChair == null) {
            return AjaxResult.error("按摩椅不存在");
        }
        massageChair.setStatus(YesOrNoEnum.YES.getCode());
        return toAjax(massageChairService.updateById(massageChair));
    }

    /**
     * 下架
     */
    @PreAuthorize("@ss.hasPermi('buss:chair:offShelf')")
    @Log(title = "按摩椅", businessType = BusinessType.UPDATE)
    @GetMapping("/offShelf/{chairId}")
    public AjaxResult offShelf(@PathVariable Long chairId) {
        MassageChair massageChair = massageChairService.getById(chairId);
        if (massageChair == null) {
            return AjaxResult.error("按摩椅不存在");
        }
        massageChair.setStatus(YesOrNoEnum.NO.getCode());
        return toAjax(massageChairService.updateById(massageChair));
    }

}
